﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Caliburn.Micro;
using WinMote7.Helpers;

namespace WinMote7.ViewModels
{
    public class PhonePlayerViewModel
    {
        readonly INavigationService navigationService;

        private bool _isPlaying;

        private Guid _rendererId;

        public PhonePlayerViewModel(INavigationService navigationService)
        {
            this.navigationService = navigationService;

            _isPlaying = true;

            var queryString = QueryStringHelper.ParseQueryString(navigationService.Source.OriginalString);
            string mediaUri= string.Empty;
            queryString.TryGetValue("MediaUri", out mediaUri);

            VideoPlayer = HttpUtility.UrlDecode(mediaUri);

            if (VideoPlayer.Contains("?"))
            {
                VideoPlayer = VideoPlayer.Substring(0, VideoPlayer.LastIndexOf('?'));
            }

            mediaUri = string.Empty;
            queryString.TryGetValue("FileName", out mediaUri);
            VideoFile = HttpUtility.UrlDecode(mediaUri);

            mediaUri = string.Empty;
            queryString.TryGetValue("RendererId", out mediaUri);
            _rendererId = new Guid(HttpUtility.UrlDecode(mediaUri));
        }

        public string VideoPlayer { get; set; }

        public string VideoFile { get; set; }

        public void SkipBack()
        {

        }

        public void Pause()
        {
            UPnPMediaBrowserServiceClient client = new UPnPMediaBrowserServiceClient();
            client.PauseAsync(_rendererId);
        }

        public void Play()
        {
            if (!MediaPlayer)
            {
                UPnPMediaBrowserServiceClient client = new UPnPMediaBrowserServiceClient();
                client.ResumePlayAsync(_rendererId);
            }
            else
            {
                // todo, stop playing on media player control
            }
        }

        public void Stop()
        {
            UPnPMediaBrowserServiceClient client = new UPnPMediaBrowserServiceClient();
            client.StopAsync(_rendererId);
        }

        public void SkipForward()
        {

        }

        public bool MediaPlayer
        {
            get
            {
                return _rendererId == Guid.Empty;
            }
        }

        //public bool Play
        //{
        //    get
        //    {
        //        return !_isPlaying;
        //    }
        //}

        //public bool Pause
        //{
        //    get
        //    {
        //        return _isPlaying;
        //    }
        //}
    }
}
